package com.erpanderp.wam.usm.model.service.timesheet;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.timesheet.ProjectAssignmentTimesheetDetail;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class ProjectAssignmentTimesheetDetailDAO {
	
	public List<ProjectAssignmentTimesheetDetail> getProjectAssignmentTimesheetDetails(Long timesheetDetailId){
		List<ProjectAssignmentTimesheetDetail> projectAssignmentTimesheetDetails = null;
		
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectAssignmentTimesheetDetail patd where patd.timesheetDetail.timesheetdetailId = :timesheetDetailId";
			Query query = session.createQuery(queryString);
			query.setLong("timesheetDetailId", timesheetDetailId);
			projectAssignmentTimesheetDetails = (List<ProjectAssignmentTimesheetDetail>) query.list();
			session.getTransaction().commit();
			return projectAssignmentTimesheetDetails;			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public ProjectAssignmentTimesheetDetail updateTimesheetDetail(ProjectAssignmentTimesheetDetail projectAssignmentTimesheetDetail) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(projectAssignmentTimesheetDetail);
			session.getTransaction().commit();
			return projectAssignmentTimesheetDetail;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean updateTimesheetDetails(List<ProjectAssignmentTimesheetDetail> projectAssignmentTimesheetDetails) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			if(projectAssignmentTimesheetDetails != null){
				for(ProjectAssignmentTimesheetDetail projectAssignmentTimesheetDetail : projectAssignmentTimesheetDetails){
					session.saveOrUpdate(projectAssignmentTimesheetDetail);		
				}
			}			
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean saveAssignmentTimesheetDetails(List<ProjectAssignmentTimesheetDetail> projectAssignmentTimesheetDetails) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			if(projectAssignmentTimesheetDetails != null){
				for(ProjectAssignmentTimesheetDetail projectAssignmentTimesheetDetail : projectAssignmentTimesheetDetails){
					session.save(projectAssignmentTimesheetDetail);		
				}
			}			
			session.getTransaction().commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String a[]){
		//System.out.println(new ProjectAssignmentTimesheetDetailDAO().getTimesheetDetails(new GregorianCalendar(2012, 5, 12).getTime(), new GregorianCalendar(2012, 5, 13).getTime(), 2l));
	}
}
